package top.alanshelby.service;

import org.springframework.stereotype.Service;
import top.alanshelby.mapper.AccountMapper;

import javax.annotation.Resource;

@Service
public class AccountServiceImpl implements AccountService {

    @Resource
    private AccountMapper accountMapper;

    @Override
    public void transfer(String from, String to, double money) throws Exception {
        // 先查询from账户的钱
        double fromMoney = accountMapper.queryMoney(from);
        // 对from账户进行扣钱操作
        accountMapper.update(from, fromMoney - money);
        //手动制造异常
        // System.out.println(1 / 0);
        // 先查询to账户的钱
        double toMoney = accountMapper.queryMoney(to);
        // 对to账户进行加钱操作
        accountMapper.update(to, toMoney + money);
    }
}
